<?php

namespace common\models\dynamic;

use Yii;

/**
 * This is the model class for table "{{%dynamic_comment_0}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $dynamic_id 动态ID
 * @property string $content 评论内容
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at
 */
class DynamicComment extends \common\base\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;
    use \common\traits\SoftDeleting;
    use \common\traits\relation\UserData;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    const STATUS_REFUSE = -1;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%dynamic_comment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'user_id', 'dynamic_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['content'], 'required'],
            [['content'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'dynamic_id' => '动态ID',
            'content' => '评论内容',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getDynamic() {
        return $this->hasOne(Dynamic::className(), ['id' => 'dynamic_id']);
    }

    public function fields() {
        return [
            'id' => function() {
                return (string) $this->id;
            },
            'nickname' => function() {
                $user = $this->user->toArray();
                return !empty($user) ? $user['nickname'] : '未知用户';
            },
            'content',
            'time' => function() {
                return (string) $this->created_at;
            },
        ];
    }

}
